﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;


namespace MulticastTV
{
    /// <summary>
    /// Export channel information from internal xml back to Aditeya format.
    /// </summary>
    class ExportToAtvlist
    {
        static public void Start(string file)
        {
            StreamWriter sw = File.CreateText(file);
            int prevchan = 0;
            StringBuilder strbuild = new StringBuilder();
            DataView dw = ChannelTable.menu.Tables["Menu"].DefaultView;
            dw.Sort = "Chan";
           
            foreach (DataRowView dr in dw)
            {
                string ip = dr[0].ToString();
                int ch = (int)dr[1];
                string name = dr[2].ToString();
                string audio = dr[4].ToString();
                string group = dr[3].ToString();
                string locked = "LOCKED=0";
                               
                if (prevchan != ch && ch != 0)
                {
                    strbuild.Append("\r\nCHANNEL=" + ch.ToString() + ";NAME=" + name + ";" + locked + ";GROUP=" + group + ";AUDIO=" + audio + "+" + ip);
                }
                else
                {
                    strbuild.Append(","+audio+"+"+ip);
                }

                prevchan = ch;
            }
            strbuild.Remove(0, 2);
            strbuild.Append("\r\n");
            sw.Write(strbuild);
            sw.Close();
        }
    }
}
